/*
 * Project Name: cms_web
 * File Name: StaticHtmlInterceptor.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月12日 下午2:11:16
 */
package com.shishuo.cms.filter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.shishuo.cms.exception.HtmlCacheFileReadException;
import com.shishuo.cms.service.ConfigService;

/**
 * 拦截前台html页面，如果静态化，直接返回文件
 * @since 2015年3月12日 下午2:11:16
 */
public class StaticHtmlInterceptor implements HandlerInterceptor
{
	@Autowired
	private ConfigService configService;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		if (configService.getConfigByKey("IS_CACHE_HTML").equals("1"))
		{
			return outHtmlFile(request, response);
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
	{
		
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
	{
		
	}
	private boolean outHtmlFile(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		// 读取文件，返回文件流
		try
		{
			//站点根目录的绝对路径  
			String basePath = request.getSession().getServletContext().getRealPath("/");
			Locale local = (Locale) request.getSession().getAttribute(
					SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
			local = (null == local) ? LocaleContextHolder.getLocale() : local;
			String requestHTML = "html/" + local.getLanguage() + this.getRequestHTML(request);
			String htmlPath = basePath + requestHTML;
			File htmlFile = new File(htmlPath);
			if (!htmlFile.getParentFile().exists())
			{
				htmlFile.getParentFile().mkdirs();
			}
			if (!htmlFile.exists())
			{
				return true;
			}
			response.setContentType("text/html;charset=UTF-8;");
			PrintWriter printWriter = response.getWriter();
			InputStreamReader read = new InputStreamReader(new
					FileInputStream(htmlFile), "UTF-8");
			BufferedReader bufferedReader =
					new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null)
			{
				printWriter.write(lineTxt + "\n");
			}
			read.close();
			printWriter.flush();
			printWriter.close();
			response.flushBuffer();
			return false;
		}
		catch (Exception e)
		{
			throw new HtmlCacheFileReadException("");
		}
	}

	private String getRequestHTML(HttpServletRequest request)
	{
		String contextPath = request.getContextPath();
		String requestURI = request.getRequestURI();
		requestURI = requestURI.replaceFirst(contextPath, "");
		requestURI += ".html";
		return requestURI;
	}
}
